# -*- coding: utf-8 -*-
'''
Created on 2009-04-13

@author: argaz
'''
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
from Dictionnaires import *
import pickle
import time

class Messagere(object):
    def __init__(self):
        self.curseur=None
        pass

class Controleur:
    def __init__(self):
        self.__connection=None
        self.__curseur=None
        self.messagere=Messagere()
        #self.connection=cx_Oracle.connect()
        #self.curseur=self.connection.cursor()
        pass
    
    def connecter(self,user,pwd,server):
        try:
            self.__connection=cx_Oracle.connect(user+'/'+pwd+'@'+server)
            self.__curseur=self.__connection.cursor()
            return [1]
        except cx_Oracle.DatabaseError, erreur:
            msgErreur=Dictionnaire.getError('BASEDONNEE',erreur.args[0].code)
            return [0,msgErreur]            
        pass
    
    def __execute(self,querry):
        try:
            self.__curseur.execute(querry)
            return [1,self.__curseur] 
        except cx_Oracle.DatabaseError, erreur:
            msgErreur=Dictionnaire.getError('SQL',erreur.args[0].code)
            return [0,msgErreur]            
        pass
        
    def obtenirChamps(self,NomTable):
        requette=('select * from '+ NomTable+' where  \'x\'=\'y\'')
        reponse=self.__execute(requette)        
        curseur=reponse[1].description
        self.messagere.curseur=curseur
        reponse=[reponse[0],pickle.dumps(self.messagere)]        
        return reponse  
        
    def obtenirTables(self):
        requette=('select table_name from user_tables')
        reponse=self.__execute(requette)
        rows=reponse[1].fetchall()
        self.messagere.curseur=rows
        reponse=[reponse[0],pickle.dumps(self.messagere)]        
        return reponse


class Server(object):
    def __init__(self):
        self.server= SimpleXMLRPCServer(("localhost", 8000),allow_none=True,\
                                            requestHandler=SimpleXMLRPCRequestHandler)
        self.server.register_introspection_functions()
        pass
    
    def register(self,cls):
        self. server.register_instance(cls)
        pass
    
    def run(self):
        print 'OK, serveur demarre a '+time.ctime(time.time())
        self.server.serve_forever()
        pass

class e:
    def __init__(self):
        self.id=1
        pass

serveur=Server()
serveur.register(Controleur())
serveur.run()  